package towa.com.remoteWMP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

import android.util.Log;

public class ReceiveThread extends Thread {

	private CommunicationService remoteWMP;
	private Socket socket;
	private boolean isRunning;
	
	public ReceiveThread(CommunicationService remoteWMP, Socket socket) {
		this.remoteWMP = remoteWMP;
		this.socket = socket;
		isRunning = true;
	}
	
	public void disableRunning() {
		isRunning = false;
	}
	
	@Override
	public void run() {
		try {
			InputStream is = socket.getInputStream();
//			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String temp = "";
			byte[] bytes = new byte[1024];
			while (isRunning) {
				int readBytes = is.read(bytes);
//				Log.i("WMPRemote", "this is spam");
				if (readBytes > 0) {
					temp = new String(bytes);
	//				temp = br.readLine();
					remoteWMP.newDataReceived(temp);
				}
			}
		} catch (IOException e) {
			Log.e("WMPRemoteConnection", e.toString());
		}
	}
	
}
